#pragma once
#include<iostream>
#include<sys/ipc.h>
#include<sys/shm.h>
#include "log.hpp"
using namespace std;
const string pathname = "/home/caugyj/9_22linux";
const int proj_id = 0x7777;
Log _log;

//大小建议是4096的整数倍
//if 4097 实际上操作系统给你的是4096 * 2的大小
const int size = 4096;
key_t GetKey()
{
    key_t k = ftok(pathname.c_str(),proj_id);
    if(k < 0)
    {
        _log(Fatal,"ftok error %s",strerror(errno));
        exit(1);
    }
    _log(Info,"ftok suceess,key is : %d",k);
    return k;
}
int GetSharedMem(int flag)
{
    key_t key = GetKey();
    int shmid = shmget(key,size,flag);
    if(shmid < 0)
    {
        _log(Fatal,"create share memory error:%s",strerror(errno));
        exit(2);
    }

    _log(Info,"create success,shmid:%d",shmid);

    return shmid;
}

int CreateShm()
{
    return GetSharedMem(IPC_CREAT|IPC_EXCL|0666);
}

int GetShm()
{
    return GetSharedMem(IPC_CREAT);
}






